package cn.tedu.cwgl.service.impl;

import cn.tedu.cwgl.entity.Debt;
import cn.tedu.cwgl.entity.UserInfo;
import cn.tedu.cwgl.mapper.DebtMapper;
import cn.tedu.cwgl.service.DebtService;
import cn.tedu.cwgl.service.UserInfoService;
import cn.tedu.cwgl.util.Config;
import cn.tedu.cwgl.util.PageModel;
import cn.tedu.cwgl.util.Result;
import cn.tedu.cwgl.util.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class DebtServiceImpl implements DebtService {

    @Resource
    private DebtMapper debtMapper;
    private UserInfoService userInfoService;

    @Override
    public Result<Debt> getDebt(PageModel model) {
        List<Debt> debts = debtMapper.getDebt(model);
//        System.out.println(debts);
        if (debts.size() >= 0) {
            Result<Debt> result = ResultUtil.success(debts);

            result.setTotal(debtMapper.getTotalDebt(model));
            if (result.getTotal() == 0) {
                result.setMsg("查询不到任何相关数据");
            } else {
                result.setMsg("数据获取成功");
            }
            return result;
        } else {
            return ResultUtil.unSuccess("数据获取失败");
        }
    }

    private UserInfo getSessionUser(HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute(Config.CURRENT_USERNAME);
        userInfo.setPassword(null);
        return userInfo;
    }

    @Override
    public int addDebt(Debt debt, HttpSession httpSession) {
//从session获取id
        UserInfo userInfo1 = getSessionUser(httpSession);


        debt.setUserid(userInfo1.getId());
        debt.setHouseid(userInfo1.getHouseid());


        int result = debtMapper.addDebt(debt);
//        System.out.println(debt);


        return result;
    }

    @Override
    public int deleteDebt(Debt debt) {
        return debtMapper.deleteDebt(debt);
    }

    @Override
    public int updateDebt(Debt debt) {
        return debtMapper.updateDebt(debt);
    }


}
